package com.ziq.zcamp.place

import android.view.View
import androidx.core.view.children
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.donkingliang.groupedadapter.holder.BaseViewHolder
import com.ziq.base.extension.click
import com.ziq.base.extension.dp
import com.ziq.zcamp.R
import com.ziq.zcamp.bean.place.DetailGroup
import com.ziq.zcamp.bean.place.PlaceScoreInfo
import com.ziq.zcamp.bean.post.ImageInfo
import com.ziq.zcamp.bean.post.PostingList
import com.ziq.zcamp.databinding.ItemPlaceDetailAppraiseBinding
import com.ziq.zcamp.mine.PersonalHomePageActivity
import com.ziq.zcamp.util.ImageUtil
import com.ziq.zcamp.view.SafetySimpleDraweeView

class PlaceDetailAppraiseGroup(val adapter: PlaceDetailAdapter) : DetailGroup() {
    override fun getHeaderViewType(groupPosition: Int): Int {
        return 0
    }

    override fun getChildViewType(groupPosition: Int, childPosition: Int): Int {
        return PlaceDetailAdapter.TYPE_CHILD_APPRAISE
    }

    override fun getChildrenCount(): Int {
        return 1
    }

    override fun hasHeader(groupPosition: Int): Boolean {
        return false
    }

    override fun hasFooter(groupPosition: Int): Boolean {
        return false
    }

    var scoreInfo: PlaceScoreInfo? = null
    var postingList: PostingList? = null

    override fun onBindHeaderViewHolder(
        holder: BaseViewHolder?,
        groupPosition: Int
    ) {
    }

    override fun onBindChildViewHolder(
        holder: BaseViewHolder?,
        groupPosition: Int,
        childPosition: Int
    ) {

        val layoutParams = holder?.itemView?.layoutParams
        if (layoutParams != null && layoutParams is StaggeredGridLayoutManager.LayoutParams) {
            layoutParams.isFullSpan = true
        }

        holder?.let {
            val binding = ItemPlaceDetailAppraiseBinding.bind(holder.itemView)
            val lp = binding.container.layoutParams as StaggeredGridLayoutManager.LayoutParams
            if(groupPosition == 0){
                lp.topMargin = 0
                binding.container.setPadding(0, 8.dp, 0, 0)
                binding.container.setBackgroundResource(R.drawable.bg_white_0_0_5_5)
            } else {
                lp.topMargin = 8.dp
                binding.container.setPadding(0, 0, 0, 0)
                binding.container.setBackgroundResource(R.drawable.bg_white_5dp)
            }
            binding.container.layoutParams = lp

            if(scoreInfo != null){
                binding.clAppraiseInfo.visibility = View.VISIBLE
                if((scoreInfo?.total?:0) > 999){
                    binding.btnAppraiseSeeAll.text = "查看全部 (999+)"
                } else {
                    binding.btnAppraiseSeeAll.text = "查看全部 (${(scoreInfo?.total?:0)})"
                }
                if(scoreInfo?.list?.isNotEmpty() == true){
                    scoreInfo?.list?.get(0)?.let { item ->
                        binding.tvAppraiseContent.text = item.content
                        binding.tvUserName.text = item.nick
                        ImageUtil.updateImageFromNetwork(binding.ivUserImage, item.portrait)

                        binding.ivUserImage.click {
                            PersonalHomePageActivity.open(holder.itemView.context, item.uid?:0)
                        }
                        binding.clAppraiseInfo.click {
//                            CampAppraiseActivity.open(holder.itemView.context,adapter.parametersGroup.placeDetail?.camp?.id?:0)
                        }
                    }
                }
            } else {
                binding.clAppraiseInfo.visibility = View.GONE
            }

            binding.vLine.visibility = View.GONE

            if(postingList?.list?.isNotEmpty() == true || (postingList?.count?:0) > 0){
                binding.clPostInfo.visibility = View.VISIBLE
                if(scoreInfo != null){
                    binding.vLine.visibility = View.VISIBLE
                }
                if((postingList?.count?:0) > 999){
                    binding.btnPostSeeAll.text = "查看全部 (999+)"
                } else {
                    binding.btnPostSeeAll.text = "查看全部 (${(postingList?.count?:0)})"
                }

                val imageInfoList = mutableListOf<ImageInfo>()
                for(item in postingList?.list!!){
                    if(item.post?.images?.isNotEmpty() == true){
                        imageInfoList.addAll(item.post?.images!!)
                    }
                    if(imageInfoList.size >= 4){
                        break
                    }
                }
                for ((index, imageView) in binding.llImages.children.withIndex()){
                    if(imageView is SafetySimpleDraweeView){
                        if(imageInfoList.isNotEmpty()){
                            if(index < (imageInfoList.size)){
                                val imageInfo = imageInfoList.get(index)
                                imageView.visibility = View.VISIBLE
                                ImageUtil.updateImageFromNetwork(imageView, imageInfo.url)
                            } else {
                                imageView.visibility = View.INVISIBLE
                                ImageUtil.updateImageFromNetwork(imageView, "")
                            }
                        } else {
                            imageView.visibility = View.INVISIBLE
                            ImageUtil.updateImageFromNetwork(imageView, "")
                        }
                    }
                }
                binding.clPostInfo.click {
                    PlacePunchActivity.open(holder.itemView.context,adapter.parametersGroup.placeDetail?.camp?.id?:0)
                }
            } else {
                binding.clPostInfo.visibility = View.GONE
            }

        }
    }

}